package com.hnust.controller;

import com.hnust.entity.code.*;
import com.hnust.entity.dto.CodeDto;
import com.hnust.service.*;
import com.hnust.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 创建人：zhangRuWang
 * 创建时间：2022/5/6
 * 描述：
 */
@RestController
@Slf4j
@CrossOrigin("*")
public class CodeController {
    @Autowired
    private DeviceCodeService deviceCodeService;
    @Autowired
    private SensorCodeService sensorCodeService;
    @Autowired
    private RegionCodeService regionCodeService;

    @GetMapping("/code/list")
    public R<List<CodeDto>> codeList(Integer type){
        List<CodeDto> codeDtos=regionCodeService.codeList(type);
        return R.success(codeDtos);
    }

    @GetMapping("/device_code/list")
    public R<List<DeviceCode>> deviceCodeList(){
        return R.success(deviceCodeService.list());
    }
    @GetMapping("/sensor_code/list")
    public R<List<SensorCode>> sensorCodeList(){
        return R.success(sensorCodeService.list());
    }
    //type=1,region
    //type=2,build
    //type=3,floor
    //type=4,room
    @GetMapping("/code/search")
    public R<List<CodeDto>> codeSearch(String codeName,Integer type){
        List<CodeDto> codeDtos=regionCodeService.codeSearch(codeName,type);
        return R.success(codeDtos);
    }
    @GetMapping("/code/delete")
    public R<String> codeDelete(String id,Integer type){
        regionCodeService.codeDelete(id,type);
        return R.success("删除成功");
    }
    @PostMapping("/code/modify")
    public R<String> codeModify(@RequestBody CodeDto codeDto){
        log.info("修改code: {}",codeDto);
        regionCodeService.codeModify(codeDto);
        return R.success("修改成功");
    }

    @PostMapping("/code/add")
    public R<String> codeAdd(@RequestBody CodeDto codeDto){
        log.info("新增code: {}",codeDto);
        regionCodeService.codeAdd(codeDto);
        return R.success("添加成功");
    }

}
